//
//  DSUnitContentView.m
//  DSJY
//
//  Created by 冯华强 on 16/9/29.
//  Copyright © 2016年 dsjy. All rights reserved.
//  单元练习内容View

#import "DSUnitContentView.h"
#import "DSUnitView.h"
#import "Masonry.h"
#import "DSUnitSubContent.h"
#import "DSUnitContentViewModel.h"
#import "DSUnitListening.h"
#import "DSUnitListeningContent.h"
#import "DSUnitBorderView.h"

#define UnitContentViewW 550
@interface DSUnitContentView ()
//居中的view（只有一张）
@property (nonatomic, weak, readwrite) DSUnitView *centerView;
//上左侧的View
@property (nonatomic, weak, readwrite) DSUnitView *topLeftView;
//上右侧的View
@property (nonatomic, weak, readwrite) DSUnitView *topRightView;
//下左侧的View
@property (nonatomic, weak, readwrite) DSUnitView *bottomLeftView;
//下右侧的View
@property (nonatomic, weak, readwrite) DSUnitView *bottomRightView;
//所有的View
@property (nonatomic, strong) NSMutableArray<DSUnitView *> *unitViewList;
@end

@implementation DSUnitContentView

#pragma mark - 懒加载
- (NSMutableArray<DSUnitView *> *)unitViewList
{
    if (_unitViewList == nil) {
        _unitViewList = [NSMutableArray<DSUnitView *> array];
    }
    return _unitViewList;
}

#pragma mark - 系统回调
//初始化
+ (__kindof DSUnitContentView *)unitContentViewWithPictures:(NSInteger)pictures
{
    DSUnitContentView *unitContentView = [[self alloc] init];
    unitContentView.pictures = pictures;
    return unitContentView;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super initWithCoder:aDecoder]) {
        //设置UI
        [self setupView];
    }
    return self;
}

#pragma mark - 设置UI
- (void)setupView
{
    //居中的view（只有一张）
    self.centerView = [self createUnitViewWithIsAddList:NO];
    
    //上左侧的View
    self.topLeftView = [self createUnitView];
    
    //上左侧的View
    self.topRightView = [self createUnitView];
    
    //下左侧的View
    self.bottomLeftView = [self createUnitView];
    
    //下左侧的View
    self.bottomRightView = [self createUnitView];
    
    [self.centerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsZero);
    }];
    
    [self.centerView.unitInnerView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.top.mas_equalTo(0);
        make.width.mas_equalTo(UnitContentViewW);  //550
        make.height.mas_equalTo(UnitContentViewW * UnitViewScale); //415
    }];

    [self.topLeftView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.top.mas_equalTo(0);
        make.right.equalTo(self.mas_right).multipliedBy(0.5);
        make.height.mas_equalTo(UnitViewH);
    }];
    
    [self.topRightView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.and.top.mas_equalTo(0);
        make.bottom.and.width.equalTo(self.topLeftView);
    }];
    
    [self.bottomLeftView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.top.equalTo(self.topLeftView.mas_bottom);
        make.size.equalTo(self.topLeftView);
    }];
    
    [self.bottomRightView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(0);
        make.top.equalTo(self.topRightView.mas_bottom);
        make.size.equalTo(self.topRightView);
    }];
}

- (DSUnitView *)createUnitView
{
    return [self createUnitViewWithIsAddList:YES];
}

- (DSUnitView *)createUnitViewWithIsAddList:(BOOL)isAddList
{
    //单元内容的View
    DSUnitView *unitView = [DSUnitView unitView];
    unitView.hidden = YES;
    [self addSubview:unitView];
    if (isAddList) {
        [self.unitViewList addObject:unitView];
    }
    return unitView;
}

- (void)setUnitContentVM:(DSUnitContentViewModel *)unitContentVM
{
    _unitContentVM = unitContentVM;
    
    //先隐藏，后显示
    self.centerView.hidden = YES;
    for (DSUnitView *unitView in self.unitViewList) {
        unitView.hidden = YES;
        unitView.unitBorderView.alpha = 0.0;
    }
    
    if (unitContentVM == nil) return;
    
    DSUnitListening *listening = unitContentVM.listening;
    NSArray<DSUnitListeningContent *> *images = listening.images;
    
    if (self.pictures == 1) {
        [self setupDataWithUnitView:self.centerView listeningContent:images[0]];
    }else{
        for (NSInteger i = 0; i < self.pictures; i++) {
            DSUnitView *unitView = self.unitViewList[i];
            [self setupDataWithUnitView:unitView listeningContent:images[i]];
        }
    }
}

- (void)setupDataWithUnitView:(DSUnitView *)unitView listeningContent:(DSUnitListeningContent *)listeningContent
{
    unitView.listeningContent = listeningContent;
    unitView.hidden = NO;
}

- (void)setStroyListeningContent:(DSUnitListeningContent *)stroyListeningContent
{
    _stroyListeningContent = stroyListeningContent;
    
    if (self.pictures == 1) {
        return;
    }
    
    for (NSInteger i = 0; i < self.pictures; i++) {
        DSUnitView *unitView = self.unitViewList[i];
        if ([unitView.listeningContent.name isEqualToString:stroyListeningContent.name]) {
            [UIView animateWithDuration:0.25 animations:^{
                for (DSUnitView *innerUnitView in self.unitViewList) {
                    if (unitView != innerUnitView) {
                        innerUnitView.unitBorderView.alpha = 0.0;
                    }else{
                        innerUnitView.unitBorderView.alpha = 1.0;
                    }
                }
            }];
        }
    }
}

@end
